// pages/integral/integral.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        points:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this;
        var points = options.integral;
        that.setData({
            points:points
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.init();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    show_toast: function (title) {
        wx.showToast({
            title: title,
            image: "../../images/warning.png"
        })
    },
    exchange: function (e) {
        var that = this;
        var types = e.currentTarget.dataset.type;
        var points=that.data.points;//会员当前积分
        var id = that.data.id;
        var params = {};
        params['id'] = id;
        params['type'] = types; 
        //20元优惠券传1,50元优惠券传2
        if (types == 1) {
            if (points < 300) {
                that.show_toast("积分不足");
                return;
            }
            wx.showModal({
                title: '积分兑换优惠券',
                content: '确认使用300积分兑换20元优惠券？',
                success: function (res) {
                    if (res.confirm) {
                        that.request(params);
                    } else if (res.cancel) {

                    }
                }
            })
        }
        if (types == 2) {
            if (points < 500) {
                that.show_toast("积分不足");
                return;
            }
            wx.showModal({
                title: '积分兑换优惠券',
                content: '确认使用500积分兑换50元优惠券？',
                success: function (res) {
                    if (res.confirm) {
                        that.request(params);
                    } else if (res.cancel) {

                    }
                }
            })
        }
    },
    init: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var id = data.id;
                that.setData({
                    id: id
                })
            }
        })
    },
    request:function(params){
        var that=this;
        wx.request({
            url: app.config() + "member/changeCouponByPoints",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.data.status == 0) {
                    wx.showToast({
                        title: '兑换成功',
                        icon: 'success'
                    })
                    var point = res.data.data.point;
                    that.setData({
                        points: point
                    })
                } else {
                    that.show_toast(res.data.data.message)
                }
            }
        })      
    }
})